@extends('master')
@section('title', trans('corebg.users.index.title'))
@section('content')
    <div class="container col-md-8 col-md-offset-2 mt-5">
        <div class="card">
            <div class="card-header ">
                <h5 class="float-left">{{ trans('corebg.users.index.title') }}
                    <a href="users/create">
                    @if (false !== array_search('manage-users', array_column(Session::get('user_permissions'), 'slug')))
                        <span class="fas fa-plus-circle" style="cursor:pointer" title="{{ trans('corebg.insert') }}"></span>
                    @endif
                    </a>
                </h5>
                <div class="clearfix"></div>
            </div>
            <div class="card-body mt-2">
                @if (session('status'))
                    <div class="alert alert-success">
                        {{ session('status') }}
                    </div>
                @endif
                @if (!$results)
                    <p>{{ trans('corebg.users.index.nousers') }}</p>
                @else
                    @include('users.load_data')
                @endif
            </div>
        </div>
    </div>
    <br>
    @include('shared.pagination')

    <script type="text/javascript">
        $(document).ready(function() {
            // nav buttons on click
            $('.page-link').on('click', function(e) {
                var url = $(this).attr('href');
                $.ajax({
                    type: 'GET',
                    url: url,
                    success: function(data) {
                        $("#load").html(data);
                    },
                });
            });
            // delete button on click
            $('.fa-trash').on('click', function(e) {
                var id = $(this).data("id");
                var token = $("meta[name='csrf-token']").attr("content");
                if (confirm("{{ trans('corebg.delconf') }}")) {
                    $.ajax({
                            url: "users/" + id,
                            type: 'DELETE',
                            data: {
                                "id": id,
                                "_token": token,
                            }
                        })
                        .done(function(data, textStatus, jqXHR) {
                            var response = JSON.parse(data);
                            if (response.status == '200') {
                                alert("{{ trans('corebg.delok') }}");
                                location.reload();
                            } else {
                                alert("{{ trans('corebg.delko') }}" +
                                    "\n" +
                                    response.message
                                );
                            }
                        })
                }
            });
        });
    </script>

@endsection
